﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleCompiler
{
    

    public class Statement
    {
        private static int _nextDebugId = 0;
        private static int NextDebugId()
        {
            return _nextDebugId++;
        }
        public int debugId = NextDebugId();
        public Statement() : this(null)
        {

        }
        public Statement(Statement statement1) : this(statement1, null)
        {

        }
        public Statement(Statement statement1, Statement statement2) : this(statement1, statement2, null)
        {

        }
        public Statement(Statement statement1, Statement statement2, Statement statement3) : this(statement1, statement2, statement3, null)
        {

        }
        public Statement(Statement statement1, Statement statement2, Statement statement3, Statement statement4)
        {
            if (statement1 != null)
            {
                statements.Add(statement1);
            }
            if (statement2 != null)
            {
                statements.Add(statement2);
            }
            if (statement3 != null)
            {
                statements.Add(statement3);
            }
            if (statement4 != null)
            {
                statements.Add(statement4);
            }
        }


        virtual public Variable Execute(RuntimeVariableScope scope)
        {            
            Variable lastVariable = Variable.None;
            foreach (var statement in statements)
            {
                lastVariable = statement.Execute(scope);
                if ((lastVariable.flag == ControlFlowFlag.Return) 
                    || (lastVariable.flag == ControlFlowFlag.For_Break)
                    || (lastVariable.flag == ControlFlowFlag.For_Continue))
                {
                    if (SilenceFlag())
                    {
                        lastVariable.WithFlag(ControlFlowFlag.Next);
                    }
                    break;
                }
            }             
            return lastVariable;
        }

        virtual public bool SilenceFlag()
        {
            return false;
        }

        private List<Statement> statements = new List<Statement>();     
        
        public void AddSubStatement(Statement statement)
        {
            statements.Add(statement);
        }
    }
}
